package com.atguigu.practice;

public class Solution {
    public int GetHeight(BinaryTree.TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = GetHeight(root.left);
        int rightHeight = GetHeight(root.right);
        return leftHeight > rightHeight? leftHeight+1 : rightHeight+1;
    }
    public boolean isBalanced(BinaryTree.TreeNode root) {
        // 空树是平衡二叉树
        if(root == null){
            return true;
        }
        // 获取root左右子树的高度
        int leftHeight = GetHeight(root.left);
        int rightHeight = GetHeight(root.right);
        // 根据平衡树的概念，检测root节点的平衡性
        if(Math.abs(rightHeight - leftHeight) >= 2){
            return false;
        }
        // 通过递归方式：检测root的左右子树是否为平衡树
        return isBalanced(root.left) && isBalanced(root.right);
    }
}
